clear;clc;
close all;%temp; for mean(CH1)-mean(ratio) pic
dataMat = [128:146];
% M = csvread("\\BEC-LAPTOP\sharedDesktop\190306\787_1.txt");
% CH1 = M(:,1);
% pa = (max(CH1)+0.08+min(CH1))/2;
% pb = (max(CH1)+0.08-min(CH1))/2;
Ratios_Data = zeros(length(dataMat), 3);%temp; for mean(CH1)-mean(ratio) pic
Ratios_Data(:, 1) = [0:10:180].*(pi/180);
% Ratios_Data(:, 1) = Mean_CH1;%temp; for mean(CH1)-mean(ratio) pic
for ii = 1:length(dataMat)
    Row_Num = dataMat(ii);
    excelRow=num2str(Row_Num);
    inputMatrix=xlsread("D:\vc\mail.ustc.edu.cn\Rb87BEC - Files\Rb87files\phase_stability\ps.xlsx",'sheet1',[excelRow ':' excelRow]);
    date = inputMatrix(1, 1);
    iniPhoto=inputMatrix(1,2);
    finPhoto=inputMatrix(1,3);
%     V = inputMatrix(1,15);
%     Ratios_Data(ii, 1) = acos((V-pa)/pb)*180/pi;
    photo=[num2str(iniPhoto) 'To' num2str(finPhoto)];
    load("D:\Rb87figures\phase_stability\"+num2str(date)+"\"+photo+"\Ratio_Mat_nobg.mat");
%     figure(ii);
%     plot(Ratio_Mat, '-*')
%     % ylim([0 0.2])
%     a = get(gcf, 'Position');
%     set(gcf, 'Position', [a(1) a(2) 1.3*a(3) 0.5*a(4)])
%     xlabel('Photo')
%     ylabel('AtomRatio')
%     title(sprintf('mean=%f, range=%f', mean(Ratio_Mat), max(Ratio_Mat)-min(Ratio_Mat)));
    Ratios_Data(ii, 2) = mean(Ratio_Mat_nobg);%temp; for mean(CH1)-mean(ratio) pic
    Ratios_Data(ii, 3) = max(Ratio_Mat_nobg)-min(Ratio_Mat_nobg);%temp; for mean(CH1)-mean(ratio) pic
end
[V, Inds] = sort(Ratios_Data(:, 1));
Ratios_mean_sorted = Ratios_Data(Inds, 2);
Ratios_std_sorted = Ratios_Data(Inds, 3);
figh = figure(100);
hold on;
load("D:\vc\Rb87PythonData\3DSOCbandData\190305\3x3x3;4x1x1x1;V_xz=4.800E_r,V_y=0.000E_r,Oxy=0.000E_r,Ozy=0.000E_r,Axis=0.000;mz=0.000to0.000E_r_v1.mat");
plot(AtomRatio_phase, AtomRatio+0.02,'LineWidth',2,'color','r');
errorbar((V-pi)/2,Ratios_mean_sorted,Ratios_std_sorted,'*','Color',[0 0 1],'marker','o',...
        'MarkerSize',14,'MarkerFaceColor',[0 0.6 1],'LineWidth',2,'MarkerEdgeColor',[0 0 1], 'capsize',0)
xlb = xlabel("$\varphi_{PD}/\pi$");
ylb = ylabel('$f$');
xlb.Interpreter = 'latex';
ylb.Interpreter = 'latex';
xticks([-0.5*pi -0.375*pi -0.25*pi -0.125*pi 0])
xticklabels({'-1','-0.5','0','0.5','1'});
ax = gca;
ax.TickLabelInterpreter = 'latex';
% xtl.Interpreter = 'latex';
xlim([-0.5*pi 0])
set(gca,'linewidth',4,'fontsize',20);
set(100,'position',[80 50 1400 500]);
save_folder = "D:\Rb87files\phase_stability\20190307\";
mkdir(char(save_folder));
saveas(100,[save_folder+sprintf("line%dto%d", dataMat(1),dataMat(end))+"_nobg.png"]);
saveas(100,[save_folder+sprintf("line%dto%d", dataMat(1),dataMat(end))+"_nobg.fig"]);
set(gcf,'Units','Centimeters');
figh.PaperPosition = [0,0,figh.Position(3:4)];
figh.PaperSize = figh.Position(3:4);
print(['C:\Users\dell\Desktop\phase_stable.pdf'],'-dpdf','-r0')